package com.shopcart.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.demo.annotation.LoginFilter;
import com.demo.bean.DataTable;
import com.demo.bean.Ret;
import com.demo.controller.CoreAdminController;
import com.demo.dialet.PaginationSupport;
import com.demo.util.JSONUtils;
import com.demo.util.StringUtil;
import com.demo.validator.AdminLoginValidate;
import com.shopcart.daoEx.model.StoreGoodsEx;
import com.shopcart.domain.Store;
import com.shopcart.domain.StoreGoods;
import com.vsd.service.StoreGoodsService;

@Controller
@RequestMapping("storeGoods")
@LoginFilter(validateClz=AdminLoginValidate.class)
public class StoreGoodsController extends CoreAdminController{
	@Autowired
	private StoreGoodsService storeGoodsService;
 
	
	@RequestMapping("/list")
	@ResponseBody
	public String storeList(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,HttpServletRequest request){
		
//		Page<StoreGoods> p = null;
//		
//		
//		if(StringUtils.isBlank(search)){
//			p= storeGoodsService.findAll(new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
//		}else{
//			p = storeGoodsService.findByNameLike("%"+search+"%", new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
//		}
//		
//		List<StoreGoods> list = new ArrayList<>();
//		for (StoreGoods goods : p) {
//			list.add(goods);
//		}
		
		PaginationSupport.byPage(start/length, length);
		List<StoreGoodsEx> list = storeGoodsService.findExList(areaCode, null, search,null,null, start/length, length);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
	
	
	@RequestMapping("/listByStore")
	@ResponseBody
	public String listByStore(HttpSession session,@RequestParam("draw") Integer draw
			,@RequestParam("start") Integer start,@RequestParam("length") Integer length
			,@RequestParam("search[value]") String search,String areaCode,Integer type,HttpServletRequest request){
		
//		Page<StoreGoods> p = null;
//		
//		
//		if(StringUtils.isBlank(search)){
//			p= storeGoodsService.findAll(new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
//		}else{
//			p = storeGoodsService.findByNameLike("%"+search+"%", new PageRequest(start/length, length,new Sort(Direction.DESC, "createTime")));
//		}
//		
//		List<StoreGoods> list = new ArrayList<>();
//		for (StoreGoods goods : p) {
//			list.add(goods);
//		}
		String storeId= "";
		String roleCode=getSessionParameter("ROLE_CODE", session);
		if("store".equals(roleCode)) storeId=getSessionParameter("id", session);
		PaginationSupport.byPage(start/length, length);
		List<StoreGoodsEx> list = storeGoodsService.findExList(areaCode, null, search,storeId,type, start/length, length);
		
		return JSONUtils.serialize(new DataTable(draw+1,PaginationSupport.getContext().getTotalCount(), list));
	}
	
	@RequestMapping("/findById")
	@ResponseBody
	public String findById(String id,HttpServletRequest request){
		StoreGoods s = storeGoodsService.findById(id);
		return JSONUtils.serialize(s);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public String save(StoreGoods goods,HttpSession session,HttpServletRequest req) throws Exception{
		if (StringUtils.isBlank(goods.getId())){
			Store s = (Store) getCurrentUser(req);
			
			goods.setId(StringUtil.getUUID());
			goods.setCreateTime(new Date());
			goods.setIsDel(0);
			goods.setIsOnline(0);
			goods.setIsRecommend(0);
			goods.setIsTop(0);
			goods.setStoreId(s.getId());
			goods.setSellBase(0);
			goods.setMode(1);
			goods.setTotal(10000);
    		storeGoodsService.save(goods);
    	}else{
    		goods.setType(null);
    		goods.setStoreId(null);
    		storeGoodsService.updateByPrimaryKeySelective(goods);
    	}
       	return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/del")
	@ResponseBody
	public String del(String id,HttpServletRequest request){
		StoreGoods goods = new StoreGoods();
		goods.setId(id);
		goods.setIsDel(1);
		storeGoodsService.updateByPrimaryKeySelective(goods);
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/exist")
	@ResponseBody
	public String unique(String gid){
		StoreGoods g = storeGoodsService.findById(gid);
		JSONObject ret = new JSONObject();
		if(g==null) ret.put("valid", false);
		else ret.put("valid", true);
		return ret.toString();
	}
	
}
